<template>
  <div class="jsbos-common-layout">
    <div class="jsbos-common-layout-center">
      <el-row class="jsbos-common-search-box" :gutter="16">
        <el-form @submit.native.prevent>
          <el-col :span="4">
            <el-form-item label="关键词">
              <el-input
                v-model="query.keyWord"
                placeholder="请输入编码或名称"
                clearable
              >
              </el-input>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="4">
            <el-form-item label="问卷标题">
              <el-input
                v-model="query.orderName"
                placeholder="请输入"
                clearable
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="问卷编码">
              <el-input
                v-model="query.orderCode"
                placeholder="请输入"
                clearable
              >
              </el-input>
            </el-form-item>
          </el-col> -->
          <template>
            <el-col :span="6">
              <el-form-item label="开始时间">
                <el-date-picker
                  v-model="query.orderStartDate"
                  type="daterange"
                  value-format="timestamp"
                  format="yyyy-MM-dd"
                  start-placeholder="开始时间"
                  end-placeholder="结束时间"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>

            <!-- <el-col :span="4">
              <el-form-item label="有效标志">
                <el-select
                  v-model="query.enabledMark"
                  placeholder="请选择"
                  clearable
                >
                  <el-option
                    v-for="(item, index) in enabledMarkOptions"
                    :key="index"
                    :label="item.fullName"
                    :value="item.id"
                    :disabled="item.disabled"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col> -->
            <el-col :span="4">
              <el-form-item label="开放类型 ">
                <el-select
                  v-model="query.orderType"
                  placeholder="请选择"
                  clearable
                >
                  <el-option
                    v-for="(item, index) in orderTypeOptions"
                    :key="index"
                    :label="item.fullName"
                    :value="item.enCode"
                    :disabled="item.disabled"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </template>
          <el-col :span="6">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="search()"
                >查询</el-button
              >
              <el-button icon="el-icon-refresh-right" @click="reset()"
                >重置</el-button
              >
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="jsbos-common-layout-main jsbos-flex-main">
        <div class="jsbos-common-head">
          <div>
            <!-- <el-button
              type="primary"
              icon="el-icon-plus"
              @click="addOrUpdateHandle()"
              >新增
            </el-button> -->
            <!-- <el-button type="text" icon="el-icon-download" @click="exportData()"
              >导出
            </el-button> -->
          </div>
          <div class="jsbos-common-head-right">
            <el-tooltip effect="dark" content="刷新" placement="top">
              <el-link
                icon="icon-ym icon-ym-Refresh JSBOS-common-head-icon"
                :underline="false"
                @click="reset()"
              />
            </el-tooltip>
            <ScreenFull isContainer />
          </div>
        </div>
        <jsbos-table
          v-loading="listLoading"
          :data="list"
          @sort-change="sortChange"
        >
          <!-- <el-table-column
            prop="questionnaireId"
            label="问卷模板"
            width="0"
            align="left"
          />
          <el-table-column
            prop="orderCode"
            label="问卷编码"
            width="0"
            align="left"
          /> -->
          <el-table-column
            prop="orderName"
            label="问卷标题"
            width="0"
            align="left"
          />

          <el-table-column
            prop="orderType"
            label="开放类型 "
            width="0"
            align="left"
          />
          <el-table-column
            prop="orderRange"
            label="内部范围"
            width="0"
            align="left"
          />

          <el-table-column
            prop="orderStartDate"
            label="时间范围"
            width="170"
            align="center"
          >
            <template slot-scope="scope">
              {{ scope.row.orderStartDate + "—" + scope.row.orderEndDate }}
            </template>
          </el-table-column>
          <el-table-column
            prop="finishPersonNumber"
            label="完成次数"
            width="0"
            align="left"
          />
          <!-- <el-table-column
            prop="orderEndDate"
            label="结束时间"
            width="0"
            align="left"
          /> -->
          <!-- <el-table-column
            label="有效标志"
            width="0"
            prop="enabledMark"
            algin="left"
          >
            <template slot-scope="scope">
              {{ scope.row.enabledMark | dynamicText(enabledMarkOptions) }}
            </template>
          </el-table-column> -->
          <el-table-column
            prop="description"
            label="问卷描述"
            width="0"
            align="left"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            prop="creatorUserId"
            label="创建用户"
            width="0"
            align="left"
          />
          <el-table-column
            prop="creatorTime"
            label="创建时间"
            width="0"
            align="left"
          />
          <el-table-column label="操作" width="150" fixed="right">
            <template slot-scope="scope">
              <el-button type="text" @click="viewResults(scope.row)"
                >查看结果
              </el-button>
            </template>
          </el-table-column>
        </jsbos-table>
        <pagination
          :total="total"
          :page.sync="listQuery.currentPage"
          :limit.sync="listQuery.pageSize"
          @pagination="initData"
        />
      </div>
    </div>
    <!-- <JSBOS-Form v-if="formVisible" ref="JSBOSForm" @refresh="refresh" />
    <ExportBox v-if="exportBoxVisible" ref="ExportBox" @download="download" />-->
    <Detail
      v-if="detailVisible"
      ref="Detail"
      @refresh="detailVisible = false"
    />
  </div>
</template>

<script>
import request from "@/utils/request";
import Detail from "./Detail.vue";
import { getDictionaryDataSelector } from "@/api/systemData/dictionary";
export default {
  components: { Detail },
  data() {
    return {
      detailVisible: false,
      orderTypeOptions: [],
      query: {
        orderType: "",
        questionnaireId: undefined,
        orderName: undefined,
        keyWord: undefined,
        orderCode: undefined,
        orderStartDate: undefined,
        orderEndDate: undefined,
        enabledMark: undefined,
      },

      list: [],
      listLoading: true,
      total: 0,
      listQuery: {
        currentPage: 1,
        pageSize: 20,
        sort: "desc",
        sidx: "creatorTime",
      },
      columnList: [
        { prop: "questionnaireId", label: "问卷模板" },
        { prop: "orderName", label: "问卷标题" },
        { prop: "orderCode", label: "问卷编码" },
        { prop: "orderRange", label: "内部包含范围" },
        { prop: "orderStartDate", label: "开始时间" },
        { prop: "orderEndDate", label: "结束时间" },
        { prop: "enabledMark", label: "有效标志" },
        { prop: "description", label: "问卷描述" },
        { prop: "creatorUserId", label: "创建用户" },
        { prop: "creatorTime", label: "创建时间" },
      ],
      enabledMarkOptions: [
        { fullName: "有效", id: "1" },
        { fullName: "无效", id: "0" },
      ],
      enabledMarkProps: { label: "fullName", value: "id" },
    };
  },
  computed: {
    menuId() {
      return this.$route.meta.modelId || "";
    },
  },
  created() {
    this.initData();
    this.getorderTypeOptions();
  },
  methods: {
    viewResults(row) {
      console.log(row);
      this.detailVisible = true;
      this.$nextTick(() => {
        this.$refs.Detail.init(row.id);
      });
    },
    getorderTypeOptions() {
      getDictionaryDataSelector("436127844121187333").then((res) => {
        this.orderTypeOptions = res.data.list;
      });
    },

    sortChange({ column, prop, order }) {
      this.listQuery.sort = order == "ascending" ? "asc" : "desc";
      this.listQuery.sidx = !order ? "" : prop;
      this.initData();
    },
    initData() {
      this.listLoading = true;
      let _query = {
        ...this.listQuery,
        ...this.query,
        menuId: this.menuId,
      };
      request({
        url: `/api/questionnaire/OqQuestionnaireOrder/getList`,
        method: "post",
        data: _query,
      }).then((res) => {
        var _list = [];
        res.data.list.forEach((item) => {
          let _data = item;
          _list.push(_data);
        });
        // for (let i = 0; i < res.data.list.length; i++) {
        //   let _data = res.data.list[i];
        //   _list.push(_data);
        // }
        this.list = _list;
        this.total = res.data.pagination.total;

        this.listLoading = false;
      });
    },
    handleDel(id) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        type: "warning",
      })
        .then(() => {
          request({
            url: `/api/questionnaire/OqQuestionnaireOrder/${id}`,
            method: "DELETE",
          }).then((res) => {
            this.$message({
              type: "success",
              message: res.msg,
              onClose: () => {
                this.initData();
              },
            });
          });
        })
        .catch(() => {});
    },

    search() {
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: "desc",
        sidx: "creatorTime",
      };
      this.initData();
    },
    refresh(isrRefresh) {
      this.formVisible = false;
      if (isrRefresh) this.reset();
    },
    reset() {
      for (let key in this.query) {
        this.query[key] = undefined;
      }
      this.search();
    },
  },
};
</script>
